﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BolsaTrabajo.BusinessEntities;

namespace BolsaTrabajo.DataAccess
{
    public class UsuarioData
    {
        private String stConnection = String.Empty;

        public UsuarioData()
        {
            stConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
        }

        public Usuario CargarEntidad(String userName)
        {
            SqlConnection cn = new SqlConnection(stConnection);
            SqlCommand cmd = cn.CreateCommand();
            cmd.CommandTimeout = 216000000;
            cmd.CommandText = "usp_Usuario_ListarPorUserName";
            cmd.CommandType = CommandType.StoredProcedure;


            SQLHelper.AddParam(ref cmd, "@userName", ParameterDirection.Input, SqlDbType.VarChar, 50, userName);

            Usuario item = new Usuario();
            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {

                    item.UserId = dr.GetGuid(dr.GetOrdinal("UserId"));
                    item.ApplicationId = dr.GetGuid(dr.GetOrdinal("ApplicationId"));
                    item.UserName = dr.GetString(dr.GetOrdinal("UserName"));
                    //item.NombreCompleto = dr.GetString(dr.GetOrdinal("NombreCompleto"));
                    item.Email = dr.GetString(dr.GetOrdinal("Email"));
                    item.IsApproved = dr.GetBoolean(dr.GetOrdinal("IsApproved"));
                    item.IsLockedOut = dr.GetBoolean(dr.GetOrdinal("IsLockedOut"));
                    item.Id = dr.GetInt32(dr.GetOrdinal("id"));
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
                cn.Dispose();
            }
            return item;
        }
    }
}
